package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;

@Service
public class SiteExamineService {

	public List<RowBase> getSiteExamineList(Map<String,String> params,PageInfo pageInfo) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		
		String sql = "select * from (select * from OPERATION_SITE_EXAMINE t"
				+ " left join (select s.name,s.wwtp_id, s.code,s.station_category,j.s_domaincode as f_type, j.s_domaincaption as flow_type,p.name as wwtp_name,gs.group_id,gs.group_name"
				+ " from BC_SITE_INFO s" + " left join (select s_domaincode, s_domaincaption"
				+ " from data_defulatdomain"
				+ " where s_domainname = 'STATION_FLOW_NAME') j on j.s_domaincode=s.flow_type"
				+ " left join sde.Ps_Wwtp p on s.wwtp_id = p.exp_no"
				+ " left join (select oga.station_id,oga.operation_group_id as group_id, og.group_name from operation_group_station_asso oga"
				+ " left join OC_GROUP og on oga.operation_group_id = og.guid) gs on s.code = gs.station_id) ms on t.station_id=ms.code) fr where 1=1";
		
		if (params.containsKey("wwtp_id")) {
			sql += " and fr.wwtp_id = @wwtp_id";
			listParam.add(new GlobalParameter("wwtp_id", params.get("wwtp_id")));
		}
		if (params.containsKey("flow_type")) {
			sql += " and fr.f_type = @flow_type";
			listParam.add(new GlobalParameter("flow_type", params.get("flow_type")));
		}
		if (params.containsKey("station_id")) {
			sql += " and fr.station_id = @station_id";
			listParam.add(new GlobalParameter("station_id", params.get("station_id")));
		}
		if (params.containsKey("water_type")) {
			sql += " and fr.station_category = @water_type";
			listParam.add(new GlobalParameter("water_type", params.get("water_type")));
		}
		if (params.containsKey("group_id")) {
			sql += " and fr.group_id = @group_id";
			listParam.add(new GlobalParameter("group_id", params.get("group_id")));
		}
		if (params.containsKey("operator")) {
			sql += " and fr.operators like '%"+params.get("operator")+"%'";
		}
		if(params.containsKey("start")){
			sql += " where fr.exam_date >= to_date('" + params.get("start") + " 00:00:00', 'YYYY-MM-DD HH24:MI:SS')";
		}
		if(params.containsKey("end")){
			sql += " and fr.exam_date <= to_date('" + params.get("end") + " 23:59:59', 'YYYY-MM-DD HH24:MI:SS')";
		}
		
		return DataUtil.getPageRow(sql, listParam, null, pageInfo);
	}
}
